//
//  FMDBManager.m
//  xpkc
//
//  Created by Tiyon on 15/11/27.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "FMDBManager.h"

@implementation FMDBManager

-(instancetype)init{
    
    if (self = [super init]) {
        
        NSString * sandBoxPath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/zty.db"];
        
        self.dataBase = [[FMDatabase alloc] initWithPath:sandBoxPath];
        
        if ( [self.dataBase open]) {
            NSLog(@"数据库打开");
            
        }
        
//        if ([self.dataBase executeUpdate:@"drop table videoCollect"]) {
//            NSLog(@"删除表成功");
//        }
        
        
        NSString * createCollectTable = @"create table if not exists videoCollect (videoId varchar(256) primary key not null,section varchar(128),row varchar(128),lecture_section varchar(256),teacher varchar(128),duration varchar(256))";
        
        if ([self.dataBase executeUpdate:createCollectTable]) {
            
            NSLog(@"建表成功");
            
        }
        // [self createTablevideoCollect];
        
        
    }
    
    return self;
}


//-(void)createTablevideoCollect{
//
////    NSString * createReadTable = @"create table if not eixts newsread (videoId varchar(128) primary key not null, duration varchar(256),image varchar(256),teacher varchar(256),time varchar(256),title varchar(256),chapter varcahr(256),section varchar(256),spark varchar(256),drm varchar(256),lecture varcahr(256))";
////    NSString * dropTable = @"drop table videoCollect";
////
////    if ([self.dataBase executeUpdate:dropTable]) {
////        NSLog(@"删除表成功");
////    }
//
//    NSString * createCollectTable = @"create table if not exists videoCollect (videoId varchar(256) primary key not null, section integer ,row integer)";
//
//    if ([self.dataBase executeUpdate:createCollectTable]) {
//
//        NSLog(@"建表成功");
//
//    }
//
//
//
//}

-(BOOL)deleteVideoCollectDataWithvideoId:(NSString *)videoId{
    
    NSString * deleteSql = @"delete from videoCollect where videoId = ?";
    NSLog(@"%@",videoId);
    if ([self.dataBase executeUpdate:deleteSql,videoId]) {
        
        NSLog(@"删除成功");
        return YES;
        
    }
    return NO;
}

-(BOOL)insertVideoCollectWithvideoId:(NSString *)videoId indexpath:(NSIndexPath *)indexpath lecture_section:(NSString *)lecture_section teacher:(NSString *)teacher duration:(NSString *)duration{
    
    NSString * insertVideoCollect = @"insert into videoCollect (videoId,section,row,lecture_section ,teacher,duration)values(?,?,?,?,?,?)";
    NSLog(@"%@--%@",videoId,lecture_section);
    
    NSString * section = [NSString stringWithFormat:@"%ld",indexpath.section];
    NSString * row = [NSString stringWithFormat:@"%ld",indexpath.row];
    
    if ([self.dataBase executeUpdate:insertVideoCollect,videoId,section,row,lecture_section,teacher,duration]) {
        NSLog(@"插入数据成功");
        return YES;
    }
    
    return NO;
    
}

-(BOOL)selectVideoCollectWithvideoId:(NSString *)videoId{
    
    NSString * selectSql = @"select * from videoCollect where videoId = ?";
    NSLog(@"%@",videoId);
    FMResultSet * set = [self.dataBase executeQuery:selectSql,videoId];
    
    
    while([set next]) {
        
        
        return YES;
        
    }
    return NO;
}


-(NSArray *)selectVideoCollectWithSection:(NSString *)section{
    
    NSString * selectSql = @"select * from videoCollect where section = ?";
    NSLog(@"%@",section);
    FMResultSet * set = [self.dataBase executeQuery:selectSql,section];
    
    NSMutableArray * array = [NSMutableArray array];
    while([set next]) {
        
        
        VideoListModel * model = [[VideoListModel alloc] init];
        // videoId,section,row,lecture_section ,teacher,duration
        model.videoId = [set stringForColumn:@"videoId"];
        model.teacher = [set stringForColumn:@"teacher"];
        model.section = [set stringForColumn:@"lecture_section"];
        model.duration = [set stringForColumn:@"duration"];
        // model.row = [set stringForColumn:@"row"];
        
        NSLog(@"%@%@",model.section,model.teacher);
        [array addObject:model];
    }
    return  array;
}

+(instancetype)shareFMDBManager{
    
    static FMDBManager * manager ;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [[FMDBManager alloc] init];
        
    });
    
    return manager;
    
}

-(NSMutableArray *)selectVideoCollectAllData{
    
    NSString * selectAll = @"select * from videoCollect";
    NSMutableArray * array = [NSMutableArray array];
    FMResultSet * set = [self.dataBase executeQuery:selectAll];
    
    while ([set next]) {
        
        VideoCollectModel * model = [[VideoCollectModel alloc] init];
        model.videoId = [set stringForColumn:@"videoId"];
        model.section = [set stringForColumn:@"section"];
        model.row = [set stringForColumn:@"row"];
        
        [array addObject:model];
        
        
    }
    
    
    return array;
    
}

@end
